<?php
class AdminModuleController extends base {
	
	public $userId;
	public $userName;
	public $groupId;
	
	public function init() {
		parent::init();
		$userId = $this->getState('adminId');
		$userName = $this->getState('adminName');
		$adminGroupId = $this->getState('adminGroupId');
		$this->userId = $userId;
		$this->userName = $userName;
		$this->groupId = $adminGroupId;
		
		if(!$userId && getgpc('c')!='login') {
			$this->message('登录超时。', GODHOUSE_DOMAIN_WWW.'admin/login.htm');
		}
		if($this->getState('adminGroupId') >= GODHOUSE_GROUP_ADMIN_LIMIT  && getgpc('c')!='login') {
			$this->message('您没有足够权限登录后台。', GODHOUSE_DOMAIN_WWW.'admin/login.htm');
		}
		if(!$this->checkPermission(getgpc('c')) && getgpc('c')!='login' && getgpc('c')!='index') {
			$this->message('您没有足够权限操作。', $_SERVER['HTTP_REFERER']);
		}
		
		
		/*指定会员登陆后1分钟定时刷新*/
		$sqluser=" 15 ";
		$users = $this->UserModel->findByPk($sqluser);
		
		if ($users[userId]==$userId){
			$shuaxin="<meta http-equiv=refresh content=\"60\">";
		}
		/*end*/
		
		
		$titles = array(
			'index'=>'后台主页',
			'system'=>'系统管理',
			'user'=>'用户管理',
			'group'=>'用户组管理',
			'order'=>'订单管理',
			'category'=>'分类管理',
			'article'=>'文章管理',
			'product'=>'产品管理',
			'gifts'=>'礼品管理',
			'swap'=>'兑换管理',
			'area'=>'地区管理',
			'logistic'=>'配送方式',
			'report'=>'报表统计',
			'permission'=>'权限设置',
			'discount'=>'折扣设置',
		);
		
		$menuItems = array(
			'system'=>1,
			'order'=>2,
			'article'=>3,
			'user'=>4,
			'group'=>4,
			'permission'=>4,
			'product'=>5,
			'gifts'=>5,
			'swap'=>5,
			'category'=>6,
			'report'=>7,
			'area'=>8,
			'logistic'=>8,
		);
		
		/*后台新用户注册有音乐*/
		$times=date('Y-m-d h:i:s');
		$times=strtotime($times);
		$timess=date('Y-m-d');
		$usersql="status='0'";
		
		$userList = $this->UserModel->findAll($usersql);
		foreach ($userList as $v){
			$creats=date('Y-m-d',$v[createdTime]);
			if ($creats>=$timess){
			$bgsoun="<bgsound src=\"/images/darkgreen/yinyue.mp3\" loop=\"1\"> ";
			}
		}
		/*end*/
		
		
		/*后台新订单有音乐*/
		$times=date('Y-m-d h:i:s');
		$times=strtotime($times);
		$timess=date('Y-m-d');
		$ordersql="orderStatus='0'";
		
		$OrderList = $this->OrderModel->findAll($ordersql);
		foreach ($OrderList as $v){
			$creats=date('Y-m-d',$v[createdTime]);
			if ($creats>=$timess){
			$bgsoun="<bgsound src=\"/images/darkgreen/yinyue.mp3\" loop=\"-1\"> ";
			}
		}
		/*end*/
		
		
		
	 
		
		
		$activeItem = $menuItems[getgpc('c')];
		$this->view->assign('bgsoun', $bgsoun);
		$this->view->assign('order', $Order);
		$this->view->assign('shuaxin', $shuaxin);
		$this->view->assign('activeItem', $activeItem);
		$this->view->assign('headerTitle', $titles[getgpc('c')]);
		$this->view->assign('adminName', $this->getState('adminName'));
	}
	
	public function checkPermission($item) {
		
		if(!$this->groupId) return false;
		
		$permissionList = $this->PermissionModel->findAll("groupId='{$this->groupId}'");
		$permission = array();
		foreach($permissionList as $v) {
			$permission[$v['item']] = $v['groupId'];
		}
		
		/*设置菜单显示*/
		$items = $this->getState('adminItems') ? unserialize($this->getState('adminItems')) :  NULL;
		if(!$items) {
			$items = array();
			foreach (Lookup::items('permission') as $k=>$v) {
				if(isset($permission[$k])) {
					$items[$k] = 1;
				} else {
					$items[$k] = 0;
				}
			}
			$this->setState('adminItems', serialize($items));
		}
		$this->view->assign('menus', $items);
		/*end*/
		
		if(isset($permission[$item])) {
			return true;
		}
		return false;
	}
	
	protected function sendSMS($message, $mobile) {
		$url = "http://s.ccme.cc/qxt/send.jsp?circle=".GODHOUSE_SMS_USER."&pwd=".GODHOUSE_SMS_PASSWD."&mobile=$mobile&service=".GODHOUSE_SMS_SERVICE."&msgid=&message=".urlencode(iconv('UTF-8', 'GBK', $message));
		$result = file_get_contents($url);
		$result = sprintf("%d", $result);
		return $result;
	}
	
	protected function _totalPrice( $where = '' ) {
		$totalPrice = $cartCount = $offerPriceSum = 0;
		if(!empty($where)) {
			$sql = "SELECT a.id AS cartId,a.productId,a.cartCount,a.arr_date,a.del_date,a.remarks,a.discount,a.ourPrice,a.promotionsPrice,a.ispromotions,a.userId,b.productNo,b.content FROM god_shoppingcart AS a JOIN god_product as b on a.productId = b.productId WHERE $where";
		} else {
			$sql = "SELECT a.id AS cartId,a.productId,a.cartCount,a.arr_date,a.del_date,a.remarks,a.discount,a.ourPrice,a.promotionsPrice,a.ispromotions,a.userId,b.productNo,b.content FROM god_shoppingcart AS a JOIN god_product as b on a.productId = b.productId WHERE a.userId = '$this->userId' AND a.settleStatus=0";
		}
		
		$shopCartArr = $this->db->fetch_all($sql);
		$i = 1;
		foreach($shopCartArr as &$v) {			
			if($v['ispromotions']==0) {
				$v['userPrice'] = number_format($v['ourPrice']*$v['discount'], 2, '.', '');//会员价格
				$v['promotions'] = '';
			} else {
				$v['userPrice'] = number_format($v['promotionsPrice'], 2, '.', '');//折扣价格
				$v['promotions'] = '<font color=red>【促销】</font>';
			}
			
			$v['xuhao'] = $i++;
			$v['contents'] = cutstr($v['content'], '16', '');
			$v['totalPrice'] = number_format($v['ourPrice']*$v['discount']*$v['cartCount'], 2, '.', '');//小计			
			$v['offerPrice'] = number_format($v['ourPrice']*$v['cartCount']-$v['totalPrice'], 2, '.', '');//优惠
			
			$totalPrice += $v['totalPrice'];//会员商品总价
			$cartCount += $v['cartCount'];//商品总数量
			$offerPriceSum += $v['offerPrice'];//总优惠价
		}
		return array(
				'shopCartArr'=>$shopCartArr,
				'totalPrice'=>number_format($totalPrice, 2, '.', ''),
				'offerPriceSum'=>number_format($offerPriceSum, 2, '.', ''),
				'cartCount'=>$cartCount
			);
	}
}